<?php

namespace Admin\Controller;

use Think\Model;
use Org\Util;

/**
 * Description of SpProductController
 *
 * @author Myron
 */
class SpProductController extends AdminController
{

    /**
     * 展示商品列表
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function index()
    {
        $name = I('name');
        $pCategoryId = I('pid');
        $pStatus = I("pStatus");

        if ($pCategoryId > 0) {
            $map['pCategoryId'] = $pCategoryId;
        }
        $map['name'] = array('like', '%' . (string)$name . '%');
        if ($pStatus != "" && $pStatus != -1) {
            $map['status'] = intval($pStatus);
        } else {
            $pStatus = -1;
        }
        $list = $this->lists('sp_product', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $pcategorylist = M('sp_pcategory')->where(array('status' => 1))->order('type')->select();
        $this->assign('pcategorylist', $pcategorylist);
        $this->assign('pCategoryId', $pCategoryId);
        $this->assign('pStatuslist', [-1, 0, 1]);
        $this->assign('pStatusValue', $pStatus);
        $this->meta_title = '展示商品列表';
        $this->display();
    }

    /**
     * 添加商品
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function add($name = '', $code = '', $stockUnitCode = '', $stockUnitMultiple = '', $pCategoryId = '', $sellPrice = '', $remark = '', $disposalShopId = '', $isNeedCupTip = '', $unit = '', $upTime = '', $downTime = '')
    {
        if (IS_POST) {
            $pinyinCod = new \Org\Util\PinyinCode();
            $pinyinCodStr = $pinyinCod->encode($name);
            $isActivty = M('sp_pcategory')->where(array('id' => $pCategoryId))->find();
            if ($isActivty['isActivity'] == 1) {
                $upTime = strtotime($upTime);
                $downTime = strtotime($downTime);
            } else {
                $upTime = 0;
                $downTime = 0;
            }
            $gr_sp_product = array(
                'id' => $id,
                'name' => $name,
                'code' => $code,
                'stockUnitCode' => $stockUnitCode,
                'stockUnitMultiple' => $stockUnitMultiple,
                'pCategoryId' => $pCategoryId,
                'upTime' => $upTime,
                'downTime' => $downTime,
                'unit' => $unit,
                'isNeedCupTip' => (int)$isNeedCupTip,
                'sellPrice' => $sellPrice,
                'instockPrice' => 0,
                'pinyinCode' => $pinyinCodStr,
                'status' => 1,
                'syncStatus' => 1,
                'remark' => $remark,
                'disposalShopId' => $disposalShopId,
                'createby' => UID,
                'createat' => NOW_TIME,
            );
            $id = M('sp_product')->add($gr_sp_product);
            if (!$id) {
                $this->error('添加失败！');
            } else {
                $this->success('添加成功！', U('index'));
            };
        } else {
            //展示商品分类列表
            $pcategorylist = M('sp_pcategory')->where(array('status' => 1))->order('type')->select();
            $pcshoplist = M('sp_shops')->where(array('status' => 1))->order('id asc')->select();
            $unitlist = C('SP_UNIT');
            $this->assign('unitlist', $unitlist);
            $this->assign('pcategorylist', $pcategorylist);
            $this->assign('pcshoplist', $pcshoplist);
            $materialpr = M('material_product')->where(array('status' => 1))->select();
            $this->assign('materialproduct',$materialpr);
            /*$sppr = M('sp_product')->where(array('status' => 1))->select();
            $this->assign('spproduct',$sppr);*/
            $this->display();
        }
    }

    /**
     * 编辑商品
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function edit($id = '', $name = '', $code = '', $pCategoryId = '', $sellPrice = '', $remark = '', $disposalShopId = '', $isNeedCupTip = '', $unit = '', $upTime = '', $downTime = '')
    {
        if (IS_POST) {
            $pinyinCod = new \Org\Util\PinyinCode();
            $pinyinCodStr = $pinyinCod->encode($name);
            $isActivty = M('sp_pcategory')->where(array('id' => $pCategoryId))->find();
            if ($isActivty['isActivity'] == 1) {
                $upTime = strtotime($upTime);
                $downTime = strtotime($downTime);
            } else {
                $upTime = 0;
                $downTime = 0;
            }
            $oldsellprice = M('sp_product')->where(array('id' => $id))->getField('sellPrice');


            $gr_sp_product = array(
                'name' => $name,
                'code' => $code,
                'pCategoryId' => $pCategoryId,
                'unit' => $unit,
                'sellPrice' => $sellPrice,
                'isNeedCupTip' => (int)$isNeedCupTip,
                'pinyinCode' => $pinyinCodStr,
                'remark' => $remark,
                'disposalShopId' => $disposalShopId,
                'upTime' => $upTime,
                'downTime' => $downTime,
                'syncStatus' => 2,
                'updateby' => UID,
                'updateat' => NOW_TIME,
            );
            if (!M('sp_product')->where(array('id' => $id))->save($gr_sp_product)) {
                $this->error('修改失败！');
            } else {
                $gr_sp_pricechangedetail = array(
                    'productId' => $id,
                    'originPrice' => $oldsellprice,
                    'currentPrice' => $sellPrice,
                    'createby' => UID,
                    'createat' => NOW_TIME,
                );
                $id = M('sp_pricechangedetail')->add($gr_sp_pricechangedetail);
                $this->success('修改成功！', U('index'));
            };
        } else {
            $pcategorylist = M('sp_pcategory')->where(array('status' => 1))->order('type')->select();
            $pcshoplist = M('sp_shops')->where(array('status' => 1))->order('id asc')->select();
            $unitlist = C('SP_UNIT');
            $this->assign('unitlist', $unitlist);
            $this->assign('pcategorylist', $pcategorylist);
            $this->assign('pcshoplist', $pcshoplist);
            $data = M('sp_product')->where(array('id' => $id))->find();
            if ($data['upTime'] == 0) {
                $data['upTime'] = '';
            } else {
                $data['upTime'] = date('Y-m-d H:i:s', $data['upTime']);
            }

            if ($data['downTime'] == 0) {
                $data['downTime'] = '';
            } else {
                $data['downTime'] = date('Y-m-d H:i:s', $data['downTime']);
            }
            $this->assign('data', $data);
            $this->display();
        }
    }

    /**
     * 商品状态修改
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function changeStatus($method = null, $id)
    {
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'forbid':
                $this->forbid('sp_product', $map);
                break;
            case 'resume':
                $this->resume('sp_product', $map);
                break;
            case 'delete':
                $this->delete('sp_product', $map);
                break;
            default:
                $this->error('sp_product');
        }
    }

    /**
     * 商品进货价格修改
     * @author 曹梦龙 <138888611@qq.com>
     */
    function changeinstockprice($id = '', $instockPrice = '')
    {
        if (IS_POST) {
            $gr_sp_product = array(
                'instockPrice' => (double)$instockPrice,
            );
            if (!M('sp_product')->where(array('id' => $id))->save($gr_sp_product)) {
                $this->ajaxReturn(array('code' => 0, 'msg' => '进货价格修改失败！'));
            } else {
                $this->ajaxReturn(array('code' => 1, 'msg' => '进货价格修改成功！'));
            };
        } else {
            echo '请勿直接访问！';
        }
    }
}
